package com.example.handler;

import com.alibaba.fastjson.JSON;
import com.example.pojo.ResponseResult;
import com.example.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 *  @author 雨安
 *  类名： AccessDeniedhandlerImpl
 *  创建时间：2024/3/30
 */
@Component //注入容器
public class AccessDeniedhandlerImpl implements AccessDeniedHandler {

    //权限失败
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        //创建对象  403
        ResponseResult responseResult = new ResponseResult(HttpStatus.FORBIDDEN.value(),"您的权限不足",null);
        String json = JSON.toJSONString(responseResult);
        //处理异常
        WebUtils.renderString(response,json);
    }


}
